package com.zrwl.poscloud.core.restapi.admin.baseInfo;
import com.alibaba.fastjson.JSONObject;
import com.zrwl.poscloud.core.annotion.AuthorityVerify.AuthorityVerify;
import com.zrwl.poscloud.core.annotion.OperationLogger.OperationLogger;
import com.zrwl.poscloud.commons.exception.ThrowableUtils;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.validator.group.Delete;
import com.zrwl.poscloud.commons.validator.group.GetList;
import com.zrwl.poscloud.commons.validator.group.Insert;
import com.zrwl.poscloud.commons.validator.group.Update;
import com.zrwl.poscloud.xo.service.AdminService;
import com.zrwl.poscloud.xo.service.DeptAdminRService;
import com.zrwl.poscloud.xo.vo.AdminVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zrwl.poscloud.xo.service.EmployeeDeptRecordService;
import com.zrwl.poscloud.xo.vo.EmployeeDeptRecordVO;
/**
 * 人员机构变动 controller api类
 *
 * @author 刘思童 AutoGenerate
 * @date 2023-02-27
 */
@RestController
@Api(value = "人员机构变动相关接口", tags = {"人员机构变动相关接口"})
@RequestMapping("/employeeDeptRecord")
@Slf4j
public class EmployeeDeptRecordRestApi {
    @Autowired
    private EmployeeDeptRecordService employeeDeptRecordService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private DeptAdminRService deptAdminRService;
    @AuthorityVerify
    @ApiOperation(value = "获取人员机构变动列表", notes = "获取人员机构变动列表", response = String.class)
    @PostMapping("/getList")
    public JSONObject getList(@Validated({GetList.class}) @RequestBody EmployeeDeptRecordVO employeeDeptRecordVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取人员机构变动列表: {}", employeeDeptRecordVO);
        AdminVO adminVO = adminService.getMe();
        if (adminVO.isCompany()) {
            if (employeeDeptRecordVO.getCompanyCode() == null)
                employeeDeptRecordVO.setCompanyCode(adminVO.getCompanyCode());
        } else {
            return ResultUtil.errorWithJSONMessage("仅商户用户支持此功能");
        }
        employeeDeptRecordVO.setDeptRoleTree(deptAdminRService.getMyDeptRoleTree());
        return ResultUtil.successWithJSONData(employeeDeptRecordService.getPageList(employeeDeptRecordVO));
    }

    @AuthorityVerify
    @ApiOperation(value = "获取人员机构变动列表", notes = "获取人员机构变动列表", response = String.class)
    @PostMapping("/getAllList")
    public String getAllList(@Validated({GetList.class}) @RequestBody EmployeeDeptRecordVO employeeDeptRecordVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取人员机构变动列表: {}", employeeDeptRecordVO);
        return ResultUtil.successWithData(employeeDeptRecordService.getAllList(employeeDeptRecordVO));
    }

    @AuthorityVerify
    @OperationLogger(value = "新增人员机构变动")    @ApiOperation(value = "新增人员机构变动", notes = "新增人员机构变动", response = String.class)
    @PostMapping("/add")
    public String add(@Validated({Insert.class}) @RequestBody EmployeeDeptRecordVO employeeDeptRecordVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("新增人员机构变动: {}", employeeDeptRecordVO);
        return employeeDeptRecordService.add(employeeDeptRecordVO);
    }

    @AuthorityVerify
    @OperationLogger(value = "修改人员机构变动")    @ApiOperation(value = "修改人员机构变动", notes = "修改人员机构变动", response = String.class)
    @PostMapping("/edit")
    public String edit(@Validated({Update.class}) @RequestBody EmployeeDeptRecordVO employeeDeptRecordVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("修改人员机构变动: {}", employeeDeptRecordVO);
        return employeeDeptRecordService.edit(employeeDeptRecordVO);
    }

    @AuthorityVerify
    @OperationLogger(value = "删除人员机构变动")    @ApiOperation(value = "删除人员机构变动", notes = "删除人员机构变动", response = String.class)
    @PostMapping("/delete")
    public String delete(@Validated({Delete.class}) @RequestBody EmployeeDeptRecordVO employeeDeptRecordVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("删除人员机构变动: {}", employeeDeptRecordVO);
        return employeeDeptRecordService.delete(employeeDeptRecordVO);
    }

}
